#===============================================================================
# Koro Business Environment
# Copyright (C) 2008  Antonio Corroppoli
#
#This file is part of Koro BE.
# 
# Koro BE is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# Koro BE is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with Koro BE; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#===============================================================================

'''
Created on 09/ago/2011

@author: antstud
'''

import wx
#import wx.lib.anchors as anchors
from koro.ui.wxgui.widgets import controls
from koro.ui.wxgui.pages import BasePage,ListPage
from koro.ui.wxgui.mellcfg import mDefaultBorder


#===============================================================================
# MovimentiMagazzinoPage
#===============================================================================
class MovimentiMagazzinoPage(ListPage):
    TITLE="Movimenti Magazzino"
    def __init__(self,*args,**kwargs):
        ListPage.__init__(self,*args,**kwargs)  
        self._configure_list(args[2])      
        #self._item_selected=-1
        #self.set_objects(objects_list)
        #self.__do_layout()
        #self.__bind()
        #self.list_ctrl.load(self._objects_list)
        #self.list_ctrl.select(0,True)
        self.SetAutoLayout(True)        
        #self.Fit()        
        #self.Layout()
    
    def _configure_list(self,objects):
        self.set_list_columns(controls.Column('Data',100,'get_data_fmt'),
                              controls.Column('Tipo Movimento',200,'get_tipo_movimento_fmt'),
                              controls.Column('Quantita',75,'quantita'),
                              controls.Column('Cod.Prod.',100,('prodotto','ID')),
                              controls.Column('Prodotto',200,('prodotto','descrizione')),
                              controls.Column('Cliente/Forn.',200,'get_intestatario_documento'),
                              controls.Column('Rif. Documento',200,'get_documento_fmt'),
                              )
        self.load_list_objects(objects)
    
    def _layout(self):#chiamato dal metodo show.....
        self.Sizer.Add(self.get_toolbar(),(0,0),flag=wx.EXPAND|wx.BOTTOM,border=3)
        self.Sizer.Add(self.list_manager.get_managed_list(),(1,0),flag=wx.LEFT|wx.RIGHT|wx.BOTTOM|wx.TOP|wx.EXPAND|wx.GROW|wx.CENTER,border=5)
        self.Sizer.AddGrowableRow(1)
        self.Sizer.AddGrowableCol(0)
        self.Layout()
        
    def _create_toolbar(self):#viene chiamato dal costruttore di BasePage...
        '''Per configurare la toolbar nelle sottoclassi, ridefinire il metodo
        _configure_toolbar().'''
        self._tb=controls.KoroMovMagazToolBar(self)        
        self._configure_toolbar()
        self._tb.realize()
#        self.GetSizer().Add(self.get_toolbar(),(0,0),flag=wx.EXPAND|wx.BOTTOM,border=3)        
        #topsizer.Add(wx.StaticLine(self),0,wx.EXPAND|wx.TOP,border=3)
        self._tb.Bind(wx.EVT_TOOL, self._carica_handler, id=wx.ID_NEW)
        self._tb.Bind(wx.EVT_TOOL, self._scarica_handler, id=wx.ID_EDIT)
        self._tb.Bind(wx.EVT_TOOL, self._on_action_delete, id=wx.ID_DELETE)
        self._tb.Bind(wx.EVT_TOOL,self._print_mov,id=wx.ID_PRINT)
    
    def _configure_toolbar(self):
        pass
    
    def _carica_handler(self,evt):
        evt.StopPropagation()
    
    def _scarica_handler(self,evt):
        evt.StopPropagation()
    
    def _print_mov(self,evt):
        evt.StopPropagation()
    
    def __do_layout_(self):
        mainsizer=self.Sizer
        fieldsizer = wx.GridBagSizer(0,0)
        #vsizer=wx.BoxSizer(wx.VERTICAL)
        #lborder = 10
        #tborder = 6
        boxsizer=wx.BoxSizer(wx.VERTICAL)        

        self.ID = controls.mTextCtrl(id=wx.NewId(), parent=self, size=(70, -1),mstyle=controls.mTE_READ_ONLY)
        boxsizer.Add(controls.CompositeCtrl(self.ID,label='ID'),0,wx.ALL,border=mDefaultBorder)
        
        self.data = controls.KDateCtrl(self,label='Data Movimento')
        boxsizer.Add(self.data,0,wx.ALL,border=mDefaultBorder)
        
        self.prodotto = controls.ComboBox(id=wx.NewId(),parent=self,size=(197, 25),style=wx.CB_READONLY,item_info='descrizione')
        boxsizer.Add(controls.CompositeCtrl(self.prodotto,label='Prodotto'),0,wx.ALL,border=mDefaultBorder)

        self.quantita = controls.mTextCtrl(id=wx.NewId(),parent=self,size=(70, -1),mstyle=controls.mTE_DIGIT_ONLY)
        boxsizer.Add(controls.CompositeCtrl(self.quantita,label='Quantita'),0,wx.ALL,border=mDefaultBorder)
        
        self.tipo_movimento = controls.ComboBox(id=wx.NewId(),parent=self,size=(197, 25),style=wx.CB_READONLY,item_info='descrizione')
        boxsizer.Add(controls.CompositeCtrl(self.tipo_movimento,label='Tipo Mov.'),0,wx.ALL,border=mDefaultBorder)
        
        self.note = controls.mTextCtrl(id=wx.NewId(),parent=self,style=wx.TE_MULTILINE)
        boxsizer.Add(controls.CompositeCtrl(self.note,label='Note'),0,wx.ALL,border=mDefaultBorder)
        
        self.set_list_columns(controls.Column('ID',70,'ID'),
                              controls.Column('Data',100,'get_data_fmt'),
                              controls.Column('Tipo Movimento',150,('tipo_movimento','descrizione')),
                              controls.Column('Quantita',75,'quantita'),
                              controls.Column('Prodotto',100,('prodotto','codice')),
                              controls.Column('Rif. Documento',200,'get_documento_fmt'),
                              )
        fieldsizer.Add(boxsizer,(0,0),flag=wx.ALL|wx.EXPAND ,border=0)#riga zero colonna 0
        fieldsizer.Add(self.list_manager.get_managed_list(),(0,1),flag=wx.ALL|wx.EXPAND ,border=1)#riga zero colonna 1        
        fieldsizer.AddGrowableRow(0)
        fieldsizer.AddGrowableCol(1)
        
        mainsizer.Add(fieldsizer,1,flag=wx.ALL|wx.EXPAND ,border=10)#riga zero colonna 1        
        self.prodotto.load(self._request_prodotti())        
        self.tipo_movimento.load(self._request_tipi_movimento())            
        mainsizer.Layout()
    
    def _request_tipi_movimento(self):
#        observer=self._get_observer()
#        observer.set_movimenti_context()
        return self._get_page_observer().action_request('get_tipi_movimento')
               
    def _request_prodotti(self):
        observer=wx.GetApp().get_observer()
        observer.set_context('PRODOTTI')
        return observer.get_objects()    
        
    def __bind(self):
        #nella nuova versione l'evento wx.EVT_LIST_ITEM_SELECTED e' leganto da BasePanel
        #self.Bind(wx.EVT_LIST_ITEM_SELECTED,self._on_item_selected)
        pass
    
    def _enable_fields(self,enable=True):
        self.tipo_movimento.Enable(enable)        
        self.quantita.Enable(enable)
        self.prodotto.Enable(enable)
        self.data.Enable(enable)
    
    def update_fields(self,dataObj):
        print 'MovimentiMagazzinoPage.update_fields'
        self._enable_fields(dataObj.is_editable())        
        
        self.ID.SetValue(dataObj.ID)
        self.tipo_movimento.set_value(dataObj.tipo_movimento.descrizione)
        self.quantita.ChangeValue(str(dataObj.quantita))
        self.prodotto.set_value(dataObj.prodotto.descrizione)
        self.data.SetValue(dataObj.data)
        self.note.SetValue(dataObj.note)
    
    def reset_fields(self):
        print 'MovimentiMagazzinoPage.reset_fields'
        self.ID.Clear()
        self.data.set_date(wx.DateTime_Now())
        self.prodotto.set_value("")
        self.quantita.Clear()                
        self.note.Clear()
        self._enable_fields()
    
    def reset_focus(self):
        self.prodotto.SetFocus()        
    
    def update_object(self,obj):
        print 'MovimentiMagazzinoPage.set_object'
        obj.set_ID(self.ID.GetValue())
        obj.data=self.data.GetValue()
        obj.set_prodotto(self.prodotto.get_selected_object())        
        obj.set_tipo_movimento(self.tipo_movimento.get_selected_object())
        obj.set_quantita(self.quantita.GetValue())
        obj.note=self.note.GetValue()
        
    def after_save(self):
        self.update()
    
    def after_delete(self):
        self.update()
    
    def on_page_focus(self):
        self.prodotto.load(self._request_prodotti())


#===============================================================================
# InventarioPage
#===============================================================================
class InventarioPage(ListPage):
    TITLE='Inventario'
    def __init__(self,*args,**kwargs):
        #wx.Panel.__init__(self,parent,*#mainsizer.Add(self._tb,(0,0))args,**kwargs)
        ListPage.__init__(self,toolbar=False,*args,**kwargs)
        self._configure_list(args[2])   
    
    def _configure_list(self,objects):
        self.set_list_columns(controls.Column('ID',70,'ID'),
                              controls.Column('Descrizione',450,'descrizione'),
                              controls.Column('Fornitore',200,'get_fornitore_principale_fmt'),
                              controls.Column('Giacenza',200,'giacenza')
                              )
        self.load_list_objects(objects)
    
    def _layout(self):#chiamato dal metodo show.....
        self.Sizer.Add(self.list_manager.get_managed_list(),(0,0),flag=wx.LEFT|wx.RIGHT|wx.BOTTOM|wx.TOP|wx.EXPAND|wx.GROW|wx.CENTER,border=5)
        self.Sizer.AddGrowableRow(0)
        self.Sizer.AddGrowableCol(0)
        self.Layout()
        